var nodes = [
  { id: 2, name: '节点2', parentId: 6 },
  { id: 3, name: '节点3', parentId: 1 },
  { id: 6, name: '节点6', parentId: null },
  { id: 4, name: '节点4', parentId: 3 },
  { id: 1, name: '节点1', parentId: 2 },
  { id: 7, name: '节点7', parentId: 2 },
  { id: 10, name: '节点10', parentId: 7 },
  { id: 9, name: '节点9', parentId: 8 },
  { id: 8, name: '节点8', parentId: 1 }
];
var result = { id: 1, chlidren: [] }
function fn(data, id, node) {
  for (var i = 0; i < data.length; i++) {
    if (data[i].parentId === id) {
      var obj = { id: data[i].id, uname: data[i].name, chlidren: [] }
      node.chlidren.push(obj)
      fn(nodes, data[i].id, obj)
    }
  }
}
fn(nodes, 1, result)
console.log(result);
